import lvgl as lv
import config
import fonts
import time
import utils
import _thread
import os
import random


class Clock:
    def __init__(self, parent):
        self.hour_label = lv.label(parent)
        self.hour_label.set_style_text_font(fonts.font_en_80, 0)
        self.hour_label.set_text("00")
        self.hour_label.set_size(80, 80)
        self.hour_label.set_pos(10, 60)

        self.minute_label = lv.label(parent)
        self.minute_label.set_style_text_font(fonts.font_en_80, 0)
        self.minute_label.set_text("00")
        self.minute_label.set_style_text_color(lv.color_hex(0xF8AD05), lv.PART.MAIN | lv.STATE.DEFAULT)
        self.minute_label.set_size(80, 80)
        self.minute_label.set_pos(92, 60)

        self.second_label = lv.label(parent)
        self.second_label.set_style_text_font(fonts.font_en_60, 0)
        self.second_label.set_text("00")
        self.second_label.set_size(60, 60)
        self.second_label.set_pos(175, 75)

        self.date_label = lv.label(parent)
        self.date_label.set_style_text_font(fonts.font_cn_20, 0)
        self.date_label.set_text("00月00日")
        self.date_label.set_size(100, 20)
        self.date_label.set_pos(8, 140)

        self.week_label = lv.label(parent)
        self.week_label.set_style_text_font(fonts.font_cn_20, 0)
        self.week_label.set_text("周一")
        self.week_label.set_size(50, 20)
        self.week_label.set_pos(110, 140)

    def update(self):
        now = time.localtime()
        week = ["一", "二", "三", "四", "五", "六", "日"]

        self.hour_label.set_text("%02d" % now[3])
        self.minute_label.set_text("%02d" % now[4])
        self.second_label.set_text("%02d" % now[5])

        self.date_label.set_text("%d月%d日" % (now[1], now[2]))
        self.week_label.set_text("周%s" % week[now[6]])


class Weather:
    icons = []

    def __init__(self, parent):
        self.icons = os.listdir("/resource/icon/weather")
        self.loc_label = lv.label(parent)
        self.loc_label.set_style_text_font(fonts.font_cn_20, 0)
        self.loc_label.set_text("暂无数据")
        self.loc_label.set_long_mode(lv.label.LONG.SCROLL_CIRCULAR)
        self.loc_label.set_size(100, 22)
        self.loc_label.set_pos(8, 8)

        self.weather_label = lv.label(parent)
        self.weather_label.set_style_text_font(fonts.font_cn_20, 0)
        self.weather_label.set_text("--")
        self.weather_label.set_style_radius(10, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_style_bg_color(lv.color_hex(0x0091FF), lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_style_bg_opa(255, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_style_pad_left(8, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_style_pad_right(8, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_style_pad_top(2, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_style_pad_bottom(2, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_label.set_pos(120, 6)

        self.weather_tips_label = lv.label(parent)
        self.weather_tips_label.set_style_text_font(fonts.font_cn_20, 0)
        self.weather_tips_label.set_text("")
        self.weather_tips_label.set_long_mode(lv.label.LONG.SCROLL_CIRCULAR)
        self.weather_tips_label.set_size(140, 22)
        self.weather_tips_label.set_pos(8, 32)

        # weather_ico = utils.read_image('/resource/icon/weather/100d.png')
        self.weather_icon = lv.img(parent)
        # self.weather_icon.set_src(weather_ico)
        self.weather_icon.set_size(48, 48)
        self.weather_icon.set_pos(184, 8)

        # y:170
        temp_ico = utils.read_image('/resource/icon/temp.png')
        self.temp_icon = lv.img(parent)
        self.temp_icon.set_src(temp_ico)
        self.temp_icon.set_size(26, 26)
        self.temp_icon.set_pos(6, 172)

        self.weather_temp_bar = lv.bar(parent)
        self.weather_temp_bar.set_value(25, lv.ANIM.OFF)
        self.weather_temp_bar.set_width(70)
        self.weather_temp_bar.set_height(8)
        self.weather_temp_bar.set_style_bg_color(lv.color_hex(0xFFFFFF), lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_bg_opa(0, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_border_color(lv.color_hex(0xFFFFFF), lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_border_opa(150, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_border_width(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_border_side(lv.BORDER_SIDE.FULL, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_pad_left(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_pad_right(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_pad_top(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_style_pad_bottom(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_temp_bar.set_pos(36, 181)

        self.weather_temp_label = lv.label(parent)
        self.weather_temp_label.set_text("00℃")
        self.weather_temp_label.set_style_text_font(fonts.font_cn_20, 0)
        self.weather_temp_label.set_height(20)
        self.weather_temp_label.set_pos(111, 175)

        # y:200
        humidity_ico = utils.read_image('/resource/icon/humidity.png')
        self.humidity_icon = lv.img(parent)
        self.humidity_icon.set_src(humidity_ico)
        self.humidity_icon.set_size(26, 26)
        self.humidity_icon.set_pos(6, 202)

        self.weather_humidity_bar = lv.bar(parent)
        self.weather_humidity_bar.set_value(25, lv.ANIM.OFF)
        self.weather_humidity_bar.set_width(70)
        self.weather_humidity_bar.set_height(8)
        self.weather_humidity_bar.set_style_bg_color(lv.color_hex(0xFFFFFF), lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_bg_opa(0, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_border_color(lv.color_hex(0xFFFFFF), lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_border_opa(150, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_border_width(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_border_side(lv.BORDER_SIDE.FULL, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_pad_left(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_pad_right(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_pad_top(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_style_pad_bottom(1, lv.PART.MAIN | lv.STATE.DEFAULT)
        self.weather_humidity_bar.set_pos(36, 211)

        self.weather_humidity_label = lv.label(parent)
        self.weather_humidity_label.set_text("00%")
        self.weather_humidity_label.set_style_text_font(fonts.font_cn_20, 0)
        self.weather_humidity_label.set_height(20)
        self.weather_humidity_label.set_pos(111, 205)

    def update_weather_icon(self, code):
        now = time.localtime()

        h = now[3]
        if 6 <= h <= 18:
            icon_name = code + "d" + ".png"
        else:
            icon_name = code + "n" + ".png"

        if icon_name in self.icons:
            weather_ico = utils.read_image('/resource/icon/weather/' + icon_name)
            self.weather_icon.set_src(weather_ico)
        else:
            weather_ico = utils.read_image("/resource/icon/weather/100d.png")
            self.weather_icon.set_src(weather_ico)

    def update(self):
        if config.SECRET_ID and config.SECRET_KEY:
            now_weather = utils.get_weather(config.WEATHER_LOC)
            if now_weather is not None:
                if config.WEATHER_CITY:
                    self.loc_label.set_text(config.WEATHER_CITY)
                else:
                    self.loc_label.set_text("暂无数据")

                self.weather_label.set_text(now_weather["weather"]["text"])

                # if now_weather["warning"] is None:
                self.weather_tips_label.set_text("空气质量：" + now_weather["aqi"]["category"])
                # else:
                #     self.weather_tips_label.set_text(now_weather["warning"]["text"])

                self.weather_temp_label.set_text("%d℃" % now_weather["weather"]["temp"])
                self.weather_humidity_label.set_text("%d%%" % now_weather["weather"]["humidity"])
                self.weather_temp_bar.set_value(now_weather["weather"]["temp"], lv.ANIM.ON)
                self.weather_humidity_bar.set_value(now_weather["weather"]["humidity"], lv.ANIM.ON)
                self.update_weather_icon(now_weather["weather"]["icon"])


class Picture:
    def __init__(self, parent):
        self.weather_icon = lv.img(parent)
        self.weather_icon.set_size(72, 90)
        self.weather_icon.set_pos(162, 140)

    def update(self):
        if utils.is_birthday():
            img = utils.read_image('/resource/birthday_cake_icon.png')
        else:
            image_list = os.listdir("/resource/images")
            img = utils.read_image(f'/resource/images/{random.choice(image_list)}')
        self.weather_icon.set_src(img)


class WeatherClock:
    last_update_weather_time = 0
    last_update_picture_time = 0

    def __init__(self):
        self.scr = lv.obj()
        self.scr.set_style_bg_color(lv.color_hex(0x000000), lv.PART.MAIN | lv.STATE.DEFAULT)

        self.clock = Clock(self.scr)
        self.weather = Weather(self.scr)
        self.picture = Picture(self.scr)
        self.loc = config.WEATHER_LOC
        self.birthday_date = config.BIRTHDAY_DATE

        lv.scr_load(self.scr)

    def update(self):
        self.clock.update()
        now = time.time()

        if self.birthday_date != config.BIRTHDAY_DATE:
            self.picture.update()
            self.birthday_date = config.BIRTHDAY_DATE
            self.last_update_picture_time = now

        if now - self.last_update_picture_time > 60:
            self.picture.update()
            self.last_update_picture_time = now

        if self.loc != config.WEATHER_LOC:
            try:
                _thread.start_new_thread(self.weather.update, ())
                self.loc = config.WEATHER_LOC
                self.last_update_weather_time = now
            except Exception as err:
                print(err)

        if now - self.last_update_weather_time > 600:
            try:
                _thread.start_new_thread(self.weather.update, ())
                self.last_update_weather_time = now
            except Exception as err:
                print(err)
